import { _decorator, Component, EventTouch, math, Node, Prefab, Rect, Size, tween, UITransform, v2, v3, Vec2, Vec3 } from 'cc';
import { PoolManager } from './PoolManager';
import { RotateItem } from './RotateItem';
import { EventManager, MyEvent } from './EventManager';
const { ccclass, property } = _decorator;

@ccclass('RotateItemManager')
export class RotateItemManager extends Component {
    public static Instance: RotateItemManager = null;

    @property(Node)
    LeftUp: Node = null;

    @property(Node)
    RightDown: Node = null;

    @property(Node)
    CreateAreas: Node = null;

    @property(Prefab)
    ItemPrefab: Prefab = null;

    @property
    MaxCount: number = 1000;

    @property(Node)
    AredNode: Node = null;

    @property(Node)
    StartNode: Node = null;

    @property(Node)
    Item: Node = null;

    private _leftUpPos: Vec2 = new Vec2();
    private _rightDownPos: Vec2 = new Vec2();
    private _areasPos: Vec3 = new Vec3();
    private _areasSize: Size = new Size();
    private _count: number = 0;
    private _curCount: number = 0;
    private _isPause: boolean = false;

    private _touchStart: Vec2 = new Vec2();

    protected onLoad(): void {
        RotateItemManager.Instance = this;

        this._leftUpPos = v2(this.LeftUp.getWorldPosition().x, this.LeftUp.getWorldPosition().y);
        this._rightDownPos = v2(this.RightDown.getWorldPosition().x, this.RightDown.getWorldPosition().y);
        this._areasPos = this.CreateAreas.getWorldPosition();
        this._areasSize = this.CreateAreas.getComponent(UITransform).contentSize;

        PoolManager.Instance.preload(this.ItemPrefab, this.MaxCount);
        this.AredNode.once(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.AredNode.once(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.AredNode.once(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        tween(this.StartNode)
            .to(1, { scale: v3(1.2, 1.2, 1.2) }, { easing: `sineIn` })
            .to(1, { scale: v3(1, 1, 1) }, { easing: `sineIn` })
            .union()
            .repeatForever()
            .start();
    }

    protected start(): void {
        // this.addItems(1);
    }

    protected update(dt: number): void {
        if (this._isPause) return;
        if (this._count > 0) {
            this.createItem();
        }
    }

    addItems(count: number) {
        this._count += count;
    }

    createItem() {
        this._count--;
        if (this._curCount > this.MaxCount) return;
        this._curCount++;
        console.error(this._curCount);

        const item: Node = PoolManager.Instance.get(this.ItemPrefab);
        item.getComponent(RotateItem).init(this.node, this.getPosInAreas());
    }

    removeItem(target: Node) {
        this._curCount--;
        PoolManager.Instance.put(target);
    }

    checkOut(pos: Vec3): boolean {
        return pos.x < this._leftUpPos.x - 100 || pos.x > this._rightDownPos.x + 100 || pos.y < this._rightDownPos.y - 100
    }

    private getPosInAreas(): Vec3 {
        const randomX: number = math.randomRange(this._areasPos.x - this._areasSize.width / 2, this._areasPos.x + this._areasSize.width / 2);
        const randomY: number = math.randomRange(this._areasPos.y - this._areasSize.height / 2, this._areasPos.y + this._areasSize.height / 2);
        return v3(randomX, randomY, this._areasPos.z);
    }

    private pause() {
        this._isPause = !this._isPause;
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.PAUSE, this.pause, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.PAUSE, this.pause, this);
    }

    onTouchStart(event: EventTouch) {
        this._touchStart = event.getUILocation();
    }

    onTouchEnd(event: EventTouch) {
        // this._touchStart = event.getUILocation();
        const parce: Vec2 = event.getUILocation().subtract(this._touchStart).normalize();
        this.Item.getComponent(RotateItem).apply(parce);
        this.AredNode.active = false;
    }
}


